package Interface.Hoteleria;

import Interface.GUIM;
import LN.Alquileres.Hospedaje_Salon;
import LN.Varios.Hosteria;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

public class Historial_Salon extends javax.swing.JPanel {
    
    private Hosteria unHosteria;
    
    public Historial_Salon(Hosteria unaHosteria) {
        this.unHosteria = unaHosteria;
        initComponents();
        iniciarInterface(unaHosteria);
        
        
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        panelCurves1 = new org.edisoncor.gui.panel.PanelCurves();
        btn_listar_historial = new javax.swing.JButton();
        btn_exit = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jXSearchField1 = new org.jdesktop.swingx.JXSearchField();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jTextField1.setEditable(false);
        jTextField1.setBackground(new java.awt.Color(153, 153, 153));
        jTextField1.setFont(new java.awt.Font("Copperplate Gothic Light", 1, 18)); // NOI18N
        jTextField1.setForeground(new java.awt.Color(255, 255, 255));
        jTextField1.setText("Historial  de Alquileres del Salón de Eventos");
        jTextField1.setFocusable(false);
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Detalles de los Alquileres"));
        jScrollPane1.setPreferredSize(null);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo de Servicio", "Huesped", "Desde", "Hasta", "Descuento", "Total"
            }
        ));
        jTable2.setPreferredSize(null);
        jScrollPane1.setViewportView(jTable2);

        btn_listar_historial.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/REAL OSX APPLICATIONS TEXT EDIT.png"))); // NOI18N
        btn_listar_historial.setText("Listar");
        btn_listar_historial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_listar_historialActionPerformed(evt);
            }
        });

        btn_exit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Shutdown.png"))); // NOI18N
        btn_exit.setText("Salir");
        btn_exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_exitActionPerformed(evt);
            }
        });

        jCheckBox1.setFont(new java.awt.Font("Bell MT", 2, 18)); // NOI18N
        jCheckBox1.setText("Listar solamente el alquiler seleccionado");

        jXSearchField1.setInstantSearchDelay(1000);
        jXSearchField1.setPrompt("Nombre del Huesped");
        jXSearchField1.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jXSearchField1CaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout panelCurves1Layout = new javax.swing.GroupLayout(panelCurves1);
        panelCurves1.setLayout(panelCurves1Layout);
        panelCurves1Layout.setHorizontalGroup(
            panelCurves1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCurves1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btn_listar_historial)
                .addGap(18, 18, 18)
                .addComponent(btn_exit)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXSearchField1, javax.swing.GroupLayout.PREFERRED_SIZE, 1, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jCheckBox1)
                .addContainerGap())
        );
        panelCurves1Layout.setVerticalGroup(
            panelCurves1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelCurves1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(panelCurves1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_listar_historial)
                    .addComponent(btn_exit)
                    .addComponent(jCheckBox1)
                    .addComponent(jXSearchField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(panelCurves1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panelCurves1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        add(jPanel1);
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
    }//GEN-LAST:event_jTextField1ActionPerformed
    
    private void btn_exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_exitActionPerformed
    }//GEN-LAST:event_btn_exitActionPerformed
    
    private void btn_listar_historialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_listar_historialActionPerformed
        
        try {
            List<Hospedaje_Salon> historial = unHosteria.getAlquileresSalon();
            HashMap<String, Object> parametros = new HashMap();
            parametros.clear();
            JasperReport jasper = JasperCompileManager.compileReport("src/Reportes/Historial_Alquileres.jrxml");
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasper, parametros, new JRBeanCollectionDataSource(historial));
            JasperViewer jviewer = new JasperViewer(jasperPrint, false);
            
            JDialog frame;
            if (SwingUtilities.getWindowAncestor(this) instanceof JDialog) {
                frame = new JDialog((JDialog) SwingUtilities.getWindowAncestor(this), true);
            } else {
                frame = new JDialog((JFrame) SwingUtilities.getWindowAncestor(this), true);
            }
            
            frame.getContentPane().removeAll();
            frame.getContentPane().add(jviewer.getContentPane());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.show();



//            jviewer.show();
        } catch (JRException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        
    }//GEN-LAST:event_btn_listar_historialActionPerformed
    
    private void jXSearchField1CaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jXSearchField1CaretUpdate
        List<Hospedaje_Salon> filtro = new LinkedList<>();
        for (Hospedaje_Salon unHospedaje_Salon : unHosteria.getAlquileresSalon()) {
            if (unHospedaje_Salon.isCheck_out()) {
                if (unHospedaje_Salon.getUnVisitante().getApellido().toUpperCase().contains(jXSearchField1.getText().toUpperCase()) || unHospedaje_Salon.getUnVisitante().getNombre().toUpperCase().contains(jXSearchField1.getText().toUpperCase())) {
                    filtro.add(unHospedaje_Salon);
                }
                
            }
        }
        GUIM.cargarTablaAlquileres(jTable2, filtro);
    }//GEN-LAST:event_jXSearchField1CaretUpdate
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_exit;
    private javax.swing.JButton btn_listar_historial;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private org.jdesktop.swingx.JXSearchField jXSearchField1;
    private org.edisoncor.gui.panel.PanelCurves panelCurves1;
    // End of variables declaration//GEN-END:variables

    private void iniciarInterface(Hosteria unaHosteria) {
        List<Hospedaje_Salon> checkOK = new LinkedList<>();
        for (Hospedaje_Salon hospedajes : unaHosteria.getAlquileresSalon()) {
            if (hospedajes.isCheck_out()) {
                checkOK.add(hospedajes);
            }
        }
        GUIM.cargarTablaAlquileres(jTable2, checkOK);
    }
}
